package com.dfbz.模拟bs服务器;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class server2 {
    public static void main(String[] args) throws IOException {
        //创建一台服务器
        ServerSocket serverSocket = new ServerSocket(9999);
        //接收一个客户端
        Socket accept = serverSocket.accept();
        InputStream inputStream = accept.getInputStream();
        byte[] data = new byte[1024];
        int len = inputStream.read(data);
        String content = new String(data,0, len);

        //GET /my.html HTTP/1.1
        String path = content.split(" ")[1].substring(1);
        //获取文件输入流
        FileInputStream fileInputStream = new FileInputStream("D:\\东方标准\\JAVASE阶段\\JAVASE-CODE\\Day20\\src\\my.html");
        byte[] bytes = new byte[8192];
        //往服务器写请求头
        OutputStream outputStream = accept.getOutputStream();
        //要想浏览器解析数据 必须写以下两句话。
        outputStream.write("HTTP/1.1 200 OK".getBytes());
        outputStream.write("\n".getBytes());
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
            outputStream.flush();
        }
        accept.close();
        serverSocket.close();
    }
}
